package com.enthusiasm.frame;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.enthusiasm.constants.SoundManager;
import com.enthusiasm.view.GameModeView;
import com.waps.AppConnect;
import com.young.R;

public class GameModeActivity extends Activity{
	 private RelativeLayout frame;
	 private ModeAdapter adapter;
	 private LinearLayout point ;
	 private int point_number;
	 private ModeGallery gallery;
	 GameModeView modeView;
	 public static int index;
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/*surfaceview*/
		frame = new RelativeLayout(this);
		adapter = new ModeAdapter(this);
		modeView = new GameModeView(this);
		frame.addView(modeView);
		/*point*/
		point = new LinearLayout(this);
		RelativeLayout.LayoutParams relative = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.WRAP_CONTENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		initPoint();
		relative.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		relative.addRule(RelativeLayout.CENTER_HORIZONTAL);
		relative.bottomMargin = getResources().getDimensionPixelSize(R.dimen.point_bottom_margin);
		point.setLayoutParams(relative);
		frame.addView(point);
		/*gallery*/
		gallery = new ModeGallery(this);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
				ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_IN_PARENT);
		gallery.setLayoutParams(lp);
		gallery.setAdapter(adapter);
		index = getModeIndex();
		if(index < point_number)
			gallery.setSelection(index);
		
		gallery.setOnItemSelectedListener(itemSelectedListener);
		gallery.setOnItemClickListener(itemClickListener);
		frame.addView(gallery);
		
		setContentView(frame);
	}
	
	/*获取当前Mode进度*/
	private int getModeIndex() {
		int index = getIntent().getIntExtra("position", -1);
		if(index != -1)
			return index;
		SharedPreferences sp = getSharedPreferences("modecfg", Context.MODE_PRIVATE);
		index = sp.getInt("mode", 1);
		return index;
	}
	
	/*gallery点击监听器*/
	OnItemClickListener itemClickListener = new OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			if(position == gallery.getSelectedItemPosition()){
				switch(position){
				case 0:
					AppConnect.getInstance(GameModeActivity.this).getPushAd(); //消息栏推送广告
					break;
				case 1:
					Intent intent0 = new Intent(GameModeActivity.this,GameModeOneActivity.class);
					startActivity(intent0);
					finish();
					overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
					break;
				case 2:
					Intent intent1= new Intent(GameModeActivity.this,GameModeTwoActivity.class);
					startActivity(intent1);
					finish();
					overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
					break;
				case 3:
					Intent intent2= new Intent(GameModeActivity.this,GameModeThreeActivity.class);
					startActivity(intent2);
					finish();
					overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
					break;
				case 4:
					Intent intent3= new Intent(GameModeActivity.this,GameModeFourActivity.class);
					startActivity(intent3);
					finish();
					overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
					break;
				case 5:
					AppConnect.getInstance(GameModeActivity.this).showOffers(GameModeActivity.this);//推荐列表接口
					break;
				}
			}
		}
	};
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			
		};
	};

	/*gallery选择监听器*/
	OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {
		public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			index = position%adapter.getCount();
			changePoint(index);
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			
		}
	};
	
	/*改变point图标*/
	private void changePoint(int index){
		point_number = adapter.getCount();
		for(int i = 0; i<point_number; i ++){
				View view = point.getChildAt(i);
				ImageView pointView = (ImageView)view;
				if(i == index)
					pointView.setBackgroundResource(R.drawable.mode_point2);
				else
					pointView.setBackgroundResource(R.drawable.mode_point1);
		}
	}
	
	/*加载点*/
	private void initPoint() {
		point_number = adapter.getCount();
		for(int i = 0; i<point_number ; i++){
			ImageView pointView = new ImageView(this);
			LinearLayout.LayoutParams linear = new LinearLayout.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
			pointView.setLayoutParams(linear);
			linear.leftMargin = getResources().getDimensionPixelSize(R.dimen.point_left_margin);
			linear.rightMargin = getResources().getDimensionPixelSize(R.dimen.point_right_margin);
			if(i==0){
        		pointView.setBackgroundResource(R.drawable.mode_point2);
        	}else
        		pointView.setBackgroundResource(R.drawable.mode_point1);
        	point.addView(pointView);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(SoundManager.getInstance(this).getIsMediaStop() == false)
			SoundManager.getInstance(this).resumeBackGroundSound();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		SoundManager.getInstance(this).pauseBackgroundSound();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		frame.removeAllViews();
		point.removeAllViews();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			Intent intent = new Intent(this,GameMenuActivity.class);
			this.startActivity(intent);
			finish();
			overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
		}
		return super.onKeyDown(keyCode, event);
	}
}
